﻿using System;
using System.Linq;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Collections.Generic;

using QuickShot.Utility;
using QuickShot.Windows;
using QuickShot.Shortcut;
using QuickShot.Reflection;
using QuickShot.Preferences;

namespace QuickShot.Forms
{
    public partial class MainForm : Form
    {
        private UserPreferences preferences = null;
        private HotkeyManager manager = null;

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            Version currentVersion = AssemblyInformation.Version.FullVersion;
            versionLabel.Text = string.Format("Version: {0}.{1}.{2}", currentVersion.Major.ToString(), currentVersion.Minor.ToString(), currentVersion.Build.ToString());
            preferences = Serializer.LoadSettings();
            manager = new HotkeyManager();
            UpdateHotkeys();
        }

        private void preferencesButton_Click(object sender, EventArgs e)
        {
            PreferencesForm preferencesForm = new PreferencesForm(preferences);
            UserPreferences oldPreferences = preferences;
            if (preferencesForm.ShowDialog() == DialogResult.OK)
            {
                if (preferencesForm.Preferences != preferences)
                {
                    preferences = preferencesForm.Preferences;
                    if ((oldPreferences.FullscreenHotkey != (preferences.FullscreenHotkey)) || (oldPreferences.RegionHotkey != preferences.RegionHotkey))
                    {
                        UpdateHotkeys();
                    }
                    Serializer.SaveSettings(preferences);
                }
            }
        }

        private void CaptureFullScreen()
        {
            Bitmap capture = null;

            if (preferences.CapturePrimaryDisplay)
            {
                capture = ScreenCapture.CaptureScreen(Screen.PrimaryScreen.Bounds.Location, Screen.PrimaryScreen.Bounds.Size);
            }
            else
            {
                capture = ScreenCapture.CaptureScreen(ScreenData.StartPoint, ScreenData.ScreenSize);
            }


        }

        private void CaptureRegion()
        {
            int tfsTest = 0;
            RegionForm regionForm = new RegionForm();
            if (regionForm.ShowDialog() == DialogResult.OK)
            {
                Bitmap b = ScreenCapture.CaptureScreen(regionForm.RegionRectangle.Location, regionForm.RegionRectangle.Size);
                b.Save(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\pic.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
            }
        }

        private void CaptureActiveWindow()
        {
            IntPtr foregroundWindow = WinAPI.GetForegroundWindow();
            WinAPI.Rect rectangle;
            WinAPI.GetWindowRect(foregroundWindow, out rectangle);
            Bitmap b = ScreenCapture.CaptureScreen(new Point(rectangle.Left, rectangle.Top), new Size(rectangle.Right - rectangle.Left, rectangle.Bottom - rectangle.Top));
            b.Save(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\pic.bmp");
        }

        private void UpdateHotkeys()
        {
            if (manager.Hotkeys != null)
            {
                foreach (var value in manager.Hotkeys)
                    value.Key.Dispose();
                manager.Hotkeys = null;
            }

            manager.Hotkeys = new Dictionary<GlobalHotkey, Action>();

            if (preferences.FullscreenHotkey != Hotkey.None & preferences.FullscreenHotkey != null)
                manager.Hotkeys.Add(new GlobalHotkey(preferences.FullscreenHotkey, manager.Handle), CaptureFullScreen);
            if (preferences.RegionHotkey != Hotkey.None & preferences.RegionHotkey != null)
                manager.Hotkeys.Add(new GlobalHotkey(preferences.RegionHotkey, manager.Handle), CaptureRegion);
            if (preferences.ActiveWindowHotkey != Hotkey.None & preferences.ActiveWindowHotkey != null)
                manager.Hotkeys.Add(new GlobalHotkey(preferences.ActiveWindowHotkey, manager.Handle), CaptureActiveWindow);
            try
            {
                manager.RegisterHotkeys();
            }
            catch (HotkeyAlreadyRegisteredException ex)
            {
                MessageBox.Show(this, string.Format("I couldn't register the hotkey combination '{0}' because another program has already registered that combination!", ex.Hotkey.Modifier.ToString().Replace(", ", " + ") + " + " + ex.Hotkey.Key),
                    "Whops", MessageBoxButtons.OK, MessageBoxIcon.Error);

                if (preferences.FullscreenHotkey == ex.Hotkey)
                    preferences.FullscreenHotkey = Hotkey.None;
                else if (preferences.RegionHotkey == ex.Hotkey)
                    preferences.RegionHotkey = Hotkey.None;
                else if (preferences.ActiveWindowHotkey == ex.Hotkey)
                    preferences.ActiveWindowHotkey = Hotkey.None;
            }
        }

        private void captureRegionMenuItem_Click(object sender, EventArgs e)
        {
            CaptureRegion();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("This is a TFS Test");
        }
    }
}
